﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BA2009.DataAccess.Context;
using System.Data;

namespace BA2009.Data.DataAccess
{
    public abstract class ReadOnlyDataContext
    {
        #region DataContext methods

        private static BA2009Context readOnlyContext;

        static object lockObject = new object();

        /// <summary>
        /// Singleton ReadOnlyContext for GETs
        /// </summary>
        /// <returns></returns>
        protected static internal BA2009Context ReadOnlyContext
        {
            get
            {
                lock (lockObject)
                {
                    if (readOnlyContext == null)
                    {
                        readOnlyContext = new BA2009Context(BA2009.Context.Constants.CONNECTION_STRING);


                    }
                }
                
                return readOnlyContext;
            }
        }

        protected static internal BA2009Context RefreshContext(object entity)
        {
            lock (lockObject)
            {
                if (readOnlyContext == null)
                {
                    readOnlyContext = new BA2009Context(BA2009.Context.Constants.CONNECTION_STRING);


                }
            }
            if (readOnlyContext.Connection.State == ConnectionState.Closed || readOnlyContext.Connection.State == ConnectionState.Broken)
                readOnlyContext.Connection.Open();

            readOnlyContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, entity);

            return readOnlyContext;
        }

        #endregion
    }
}
